package com.www.applyword.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.my.ResetPayActivity;
import com.www.applyword.utils.CommonUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import java.util.HashMap;
import java.util.Map;

/**
 * author : huang
 * date   : 2019/11/28 0028 17:44
 * version: 1.0
 */
public class PayDialogFragment extends DialogFragment implements View.OnClickListener {

    private EditText et_password;

    private String alreadyInputPassword;
    private boolean isFirstInputPassword = true;
    private boolean isCanClear = true;

    public PayDialogFragment() {

    }

    public static PayDialogFragment newInstance(String tittle) {
        PayDialogFragment fragment = new PayDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tittle", tittle);
        fragment.setArguments(bundle);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.popwindow_validate_pay_password, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View contentView, @Nullable Bundle savedInstanceState) {
        String tittle = getArguments().getString("tittle");
        et_password = contentView.findViewById(R.id.et_password);
        final TextView tv_pwd_one = contentView.findViewById(R.id.tv_pwd_one);
        final TextView tv_pwd_two = contentView.findViewById(R.id.tv_pwd_two);
        final TextView tv_pwd_three = contentView.findViewById(R.id.tv_pwd_three);
        final TextView tv_pwd_four = contentView.findViewById(R.id.tv_pwd_four);
        final TextView tv_pwd_five = contentView.findViewById(R.id.tv_pwd_five);
        final TextView tv_pwd_six = contentView.findViewById(R.id.tv_pwd_six);
        final ImageView iv_close = contentView.findViewById(R.id.iv_close);
        final TextView tv_forget_password = contentView.findViewById(R.id.tv_forget_password);
        final TextView tv_money = contentView.findViewById(R.id.tv_money);
        tv_money.setText(tittle);
        tv_pwd_one.setOnClickListener(this);
        tv_pwd_two.setOnClickListener(this);
        tv_pwd_three.setOnClickListener(this);
        tv_pwd_four.setOnClickListener(this);
        tv_pwd_five.setOnClickListener(this);
        tv_pwd_six.setOnClickListener(this);
        iv_close.setOnClickListener(this);
        tv_forget_password.setOnClickListener(this);

        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                switch (s.length()) {
                    case 0:
                        if (isCanClear) {
                            tv_pwd_one.setText("");
                        }
                        break;
                    case 1:
                        tv_pwd_one.setText(s.toString().substring(0, 1));
                        tv_pwd_two.setText("");
                        break;
                    case 2:
                        tv_pwd_two.setText(s.toString().substring(1, 2));
                        tv_pwd_three.setText("");
                        break;
                    case 3:
                        tv_pwd_three.setText(s.toString().substring(2, 3));
                        tv_pwd_four.setText("");
                        break;
                    case 4:
                        tv_pwd_four.setText(s.toString().substring(3, 4));
                        tv_pwd_five.setText("");
                        break;
                    case 5:
                        tv_pwd_five.setText(s.toString().substring(4, 5));
                        tv_pwd_six.setText("");
                        break;
                    case 6:
                        if (!isFirstInputPassword) {
                            if (s.toString().equals(alreadyInputPassword)) {
                                //密码通过
                                setPayPwd(alreadyInputPassword);
                            } else {
                                //
                            }
                        }
                        tv_pwd_six.setText(s.toString().substring(5, 6));
                        if (isFirstInputPassword) {
                            alreadyInputPassword = s.toString();
                            if (s.toString().equals(alreadyInputPassword))setPayPwd(alreadyInputPassword);
                            // isFirstInputPassword = false;
                        }

                        //  Utils.showToastSafe("支付密码输入错误");
                        tv_pwd_one.setText("");
                        tv_pwd_two.setText("");
                        tv_pwd_three.setText("");
                        tv_pwd_four.setText("");
                        tv_pwd_five.setText("");
                        tv_pwd_six.setText("");
                        isCanClear = false;
                        et_password.setText("");
                        isCanClear = true;
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        super.onViewCreated(contentView, savedInstanceState);
    }

    private void setPayPwd(String alreadyInputPassword) {
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Pwd", alreadyInputPassword);
        HttpClient.get(getActivity(), Constants.VERIFICATIONPWDBY, params, new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
              //  mValidatePayPasswordPopWindow.dismiss();
             /*   if (order_state == 12){
                    Map<String, Object> params = new HashMap<>();
                    params.put("OrderID", order_id1);
                    params.put("UserID", SPUtil.getString(Constants.USERID));
                    params.put("OldOrderID", order_id1);
                    HttpClient.get(getActivity(), Constants.TURNOUTORDERMONEY, params, new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            Utils.showToastSafe("支付成功");
                            sbApply.setVisibility(View.INVISIBLE);
                            MessageEvent messageEvent = new MessageEvent();
                            messageEvent.setMessageKey(EventConstants.PAY);
                            EventBus.getDefault().post(messageEvent);
                            //  getOrderInfo();
                        }
                    });
                }else if (order_state == 100){
                    Map<String, Object> params = new HashMap<>();
                    params.put("OrderID", order_id1);
                    params.put("UserID", SPUtil.getString(Constants.USERID));
                    HttpClient.get(getActivity(), Constants.PAYMENTSERVICECHARGEMONEY, params, new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            Utils.showToastSafe("支付成功");
                            sbApply.setVisibility(View.INVISIBLE);
                            MessageEvent messageEvent = new MessageEvent();
                            messageEvent.setMessageKey(EventConstants.PAY);
                            EventBus.getDefault().post(messageEvent);
                            //    getOrderInfo();
                        }
                    });
                }else {
                    Map<String, Object> params = new HashMap<>();
                    params.put("OrderID", order_id1);
                    params.put("UserID", SPUtil.getString(Constants.USERID));
                    params.put("PayPwd", alreadyInputPassword);
                    HttpClient.get(getActivity(), Constants.CALCULATIONMONEY, params, new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            Utils.showToastSafe("支付成功");
                            sbApply.setVisibility(View.INVISIBLE);
                            MessageEvent messageEvent = new MessageEvent();
                            messageEvent.setMessageKey(EventConstants.PAY);
                            EventBus.getDefault().post(messageEvent);
                            //    getOrderInfo();
                        }
                    });
                }*/
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_pwd_one:
            case R.id.tv_pwd_two:
            case R.id.tv_pwd_three:
            case R.id.tv_pwd_four:
            case R.id.tv_pwd_five:
            case R.id.tv_pwd_six:
                CommonUtil.showKeyboard(getActivity(), et_password, true);
                break;
            case R.id.iv_close:
             //   mValidatePayPasswordPopWindow.dismiss();
                break;
            case R.id.tv_forget_password:
                Utils.startActivity(ResetPayActivity.class);
             //   mValidatePayPasswordPopWindow.dismiss();
                break;
            default:
                break;
        }
    }
}
